<?php

/*
 * Class to handle the hierarchy
 */

class Hierarchy {

  //Properties

  /*
   * Id of the hierarchy in the database
   */

  public $id = null;

  /*
   * Name of the hierarchy
   */

  public $name = null;

  /*
   * Image source
   */

  public $image = null;

  /*
   * groups the element belongs to
   */

  public $groups = null;

  public function __construct( $data=array() ) {
    if ( isset( $data['id'] ) ) $this->id = (int)$data['id'];
    if ( isset( $data['name'] ) ) $this->name = $data['name'];
	  if ( isset( $data['image'] ) ) $this->image = $data['image'];
	  if ( isset( $data['groups'] ) ) $this->groups = $data['groups'];
	}

	public function storeFormValues( $params ) {
	  $this->__construct( $params );
	}

	public static function getById( $id ) {
	  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT * FROM hierarchy WHERE id = :id";
    $st = $conn->prepare( $sql );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $row = $st->fetch();
    $conn = null;
    if ( $row ) return new Hierarchy( $row );
	}

  public static function getByGroup( $groups ) {
	  $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "SELECT * FROM hierarchy WHERE groups = :groups";
      $st = $conn->prepare( $sql );
      $st->bindValue( ":groups", $groups, PDO::PARAM_STR );
      $st->execute();
      $list = array();

	  while ( $row = $st->fetch() ) {
	    $hierarchy = new hierarchy( $row );
	    $list[] = $hierarchy;
      }

      $conn = null;
      return ( array ( "results" => $list ) );
	}

	public static function getList( $numRows=1000000) {
      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM hierarchy
              LIMIT :numRows";

      $st = $conn->prepare( $sql );
      $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
      $st->execute();
      $list = array();

      while ( $row = $st->fetch() ) {
        $hierarchy = new hierarchy( $row );
        $list[] = $hierarchy;
      }

      // Now get the total number of home that matched the criteria
      $sql = "SELECT FOUND_ROWS() AS totalRows";
      $totalRows = $conn->query( $sql )->fetch();
      $conn = null;
      return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );

	}

	public function insert() {

	    // Does the home object already have an ID?
	    if ( !is_null( $this->id ) ) trigger_error ( "hierarchy::insert(): Attempt to insert an hierarchy object that already has its ID property set (to $this->id).", E_USER_ERROR );

	    // Insert the home
	    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
	    $this->id = $conn->lastInsertId();
	    $sql = "INSERT INTO hierarchy ( id, name, image, groups ) VALUES ( :id, :name, :image, :groups )";
	    $st = $conn->prepare ( $sql );
	    $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
        $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
	    $st->bindValue( ":image", $this->image, PDO::PARAM_STR );
	    $st->bindValue( ":groups", $this->groups, PDO::PARAM_STR );
	    $st->execute();
	    $conn = null;
    }

    public function update() {
      if ( is_null( $this->id ) ) trigger_error ( "hierarchy::update(): Attempt to update an hierarchy object that does not have its ID property set.", E_USER_ERROR );

      $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
      $sql = "UPDATE hierarchy SET name=:name, image=:image, groups=:groups WHERE id = :id";
      $st = $conn->prepare ( $sql );
      $st->bindValue( ":name", $this->name, PDO::PARAM_STR );
      $st->bindValue( ":image", $this->image, PDO::PARAM_STR );
      $st->bindValue( ":groups", $this->groups, PDO::PARAM_STR );
      $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
      $st->execute();
      $conn = null;
    }

    public function delete() {
      if ( is_null( $this->id ) ) trigger_error ( "hierarchy::delete(): Attempt to delete an hierarchy object that does not have an ID property.", E_USER_ERROR );

      if(unlink( $this->image ) ) {
        $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $sql = "DELETE FROM hierarchy WHERE id=:id LIMIT 1";
        $st = $conn->prepare( $sql );
        $st->bindValue( ":id", $this->id, PDO::PARAM_INT );
        $st->execute();
        $conn = null;
      } else {
        trigger_error( "hierarchy::delete() : Attempt to delete the image failed.", E_USER_ERROR);
      }
    }
  }

?>
